using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Media;
namespace Microsoft.Phone.Controls
{
	[TemplatePart(Name = "NotificationBlock", Type = typeof(TextBlock)), TemplatePart(Name = "MessageBlock", Type = typeof(TextBlock)), TemplatePart(Name = "BackTitleBlock", Type = typeof(TextBlock)), TemplateVisualState(Name = "Semiexpanded", GroupName = "ImageState"), TemplateVisualState(Name = "Collapsed", GroupName = "ImageState"), TemplateVisualState(Name = "Expanded", GroupName = "ImageState"), TemplateVisualState(Name = "Flipped", GroupName = "ImageState")]
	public class HubTile : Control
	{
		private const string ImageStates = "ImageState";
		private const string Expanded = "Expanded";
		private const string Semiexpanded = "Semiexpanded";
		private const string Collapsed = "Collapsed";
		private const string Flipped = "Flipped";
		private const string NotificationBlock = "NotificationBlock";
		private const string MessageBlock = "MessageBlock";
		private const string BackTitleBlock = "BackTitleBlock";
		private TextBlock _notificationBlock;
		private TextBlock _messageBlock;
		private TextBlock _backTitleBlock;
		internal int _stallingCounter;
		internal bool _canDrop;
		internal bool _canFlip;
		public static readonly DependencyProperty SourceProperty = DependencyProperty.Register("Source", typeof(ImageSource), typeof(HubTile), new PropertyMetadata(null));
		public static readonly DependencyProperty TitleProperty = DependencyProperty.Register("Title", typeof(string), typeof(HubTile), new PropertyMetadata(string.Empty, new PropertyChangedCallback(HubTile.OnTitleChanged)));
		public static readonly DependencyProperty NotificationProperty = DependencyProperty.Register("Notification", typeof(string), typeof(HubTile), new PropertyMetadata(string.Empty, new PropertyChangedCallback(HubTile.OnBackContentChanged)));
		public static readonly DependencyProperty MessageProperty = DependencyProperty.Register("Message", typeof(string), typeof(HubTile), new PropertyMetadata(string.Empty, new PropertyChangedCallback(HubTile.OnBackContentChanged)));
		public static readonly DependencyProperty DisplayNotificationProperty = DependencyProperty.Register("DisplayNotification", typeof(bool), typeof(HubTile), new PropertyMetadata(false, new PropertyChangedCallback(HubTile.OnBackContentChanged)));
		public static readonly DependencyProperty IsFrozenProperty = DependencyProperty.Register("IsFrozen", typeof(bool), typeof(HubTile), new PropertyMetadata(false, new PropertyChangedCallback(HubTile.OnIsFrozenChanged)));
		public static readonly DependencyProperty GroupTagProperty = DependencyProperty.Register("GroupTag", typeof(string), typeof(HubTile), new PropertyMetadata(string.Empty));
		private static readonly DependencyProperty StateProperty = DependencyProperty.Register("State", typeof(ImageState), typeof(HubTile), new PropertyMetadata(ImageState.Expanded, new PropertyChangedCallback(HubTile.OnImageStateChanged)));
		public ImageSource Source
		{
			get
			{
				return (ImageSource)base.GetValue(HubTile.SourceProperty);
			}
			set
			{
				base.SetValue(HubTile.SourceProperty, value);
			}
		}
		public string Title
		{
			get
			{
				return (string)base.GetValue(HubTile.TitleProperty);
			}
			set
			{
				base.SetValue(HubTile.TitleProperty, value);
			}
		}
		public string Notification
		{
			get
			{
				return (string)base.GetValue(HubTile.NotificationProperty);
			}
			set
			{
				base.SetValue(HubTile.NotificationProperty, value);
			}
		}
		public string Message
		{
			get
			{
				return (string)base.GetValue(HubTile.MessageProperty);
			}
			set
			{
				base.SetValue(HubTile.MessageProperty, value);
			}
		}
		public bool DisplayNotification
		{
			get
			{
				return (bool)base.GetValue(HubTile.DisplayNotificationProperty);
			}
			set
			{
				base.SetValue(HubTile.DisplayNotificationProperty, value);
			}
		}
		public bool IsFrozen
		{
			get
			{
				return (bool)base.GetValue(HubTile.IsFrozenProperty);
			}
			set
			{
				base.SetValue(HubTile.IsFrozenProperty, value);
			}
		}
		public string GroupTag
		{
			get
			{
				return (string)base.GetValue(HubTile.GroupTagProperty);
			}
			set
			{
				base.SetValue(HubTile.GroupTagProperty, value);
			}
		}
		internal ImageState State
		{
			get
			{
				return (ImageState)base.GetValue(HubTile.StateProperty);
			}
			set
			{
				base.SetValue(HubTile.StateProperty, value);
			}
		}
		private static void OnTitleChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
		{
			HubTile hubTile = (HubTile)obj;
			if (string.IsNullOrEmpty((string)e.get_NewValue()))
			{
				hubTile._canDrop = false;
				hubTile.State = ImageState.Expanded;
				return;
			}
			hubTile._canDrop = true;
		}
		private static void OnBackContentChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
		{
			HubTile hubTile = (HubTile)obj;
			if ((!string.IsNullOrEmpty(hubTile.Notification) && hubTile.DisplayNotification) || (!string.IsNullOrEmpty(hubTile.Message) && !hubTile.DisplayNotification))
			{
				hubTile._canFlip = true;
				return;
			}
			hubTile._canFlip = false;
			hubTile.State = ImageState.Expanded;
		}
		private static void OnIsFrozenChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
		{
			HubTile tile = (HubTile)obj;
			if ((bool)e.get_NewValue())
			{
				HubTileService.FreezeHubTile(tile);
				return;
			}
			HubTileService.UnfreezeHubTile(tile);
		}
		private static void OnImageStateChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
		{
			((HubTile)obj).UpdateVisualState();
		}
		private void UpdateVisualState()
		{
			string text;
			switch (this.State)
			{
			case ImageState.Expanded:
				text = "Expanded";
				break;
			case ImageState.Semiexpanded:
				text = "Semiexpanded";
				break;
			case ImageState.Collapsed:
				text = "Collapsed";
				break;
			case ImageState.Flipped:
				text = "Flipped";
				break;
			default:
				text = "Expanded";
				break;
			}
			VisualStateManager.GoToState(this, text, true);
		}
		public override void OnApplyTemplate()
		{
			base.OnApplyTemplate();
			this._notificationBlock = (base.GetTemplateChild("NotificationBlock") as TextBlock);
			this._messageBlock = (base.GetTemplateChild("MessageBlock") as TextBlock);
			this._backTitleBlock = (base.GetTemplateChild("BackTitleBlock") as TextBlock);
			if (this._notificationBlock != null)
			{
				Binding binding = new Binding();
				binding.set_Source(this);
				binding.set_Path(new PropertyPath("DisplayNotification", new object[0]));
				binding.set_Converter(new VisibilityConverter());
				binding.set_ConverterParameter(false);
				this._notificationBlock.SetBinding(UIElement.VisibilityProperty, binding);
			}
			if (this._messageBlock != null)
			{
				Binding binding2 = new Binding();
				binding2.set_Source(this);
				binding2.set_Path(new PropertyPath("DisplayNotification", new object[0]));
				binding2.set_Converter(new VisibilityConverter());
				binding2.set_ConverterParameter(true);
				this._messageBlock.SetBinding(UIElement.VisibilityProperty, binding2);
			}
			if (this._backTitleBlock != null)
			{
				Binding binding3 = new Binding();
				binding3.set_Source(this);
				binding3.set_Path(new PropertyPath("Title", new object[0]));
				binding3.set_Converter(new MultipleToSingleLineStringConverter());
				this._backTitleBlock.SetBinding(TextBlock.TextProperty, binding3);
			}
			this.UpdateVisualState();
		}
		public HubTile()
		{
			base.set_DefaultStyleKey(typeof(HubTile));
			base.add_Loaded(new RoutedEventHandler(this.HubTile_Loaded));
			base.add_Unloaded(new RoutedEventHandler(this.HubTile_Unloaded));
		}
		private void HubTile_Loaded(object sender, RoutedEventArgs e)
		{
			HubTileService.InitializeReference(this);
		}
		private void HubTile_Unloaded(object sender, RoutedEventArgs e)
		{
			HubTileService.FinalizeReference(this);
		}
	}
}
